package com.qianyu.project.core.utils;

import com.qianyu.project.core.bean.FlowNode;
import com.qianyu.project.core.bean.Form;
import com.qianyu.project.core.service.IFlowNodeService;
import com.qianyu.project.core.service.IFormService;
import org.springframework.util.ObjectUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @author gs
 * @date create in 2021/1/7 11:19
 */
public class FlowCheckUtil {

    private static final String GREATE_EQUALS = ">=";
    private static final String LESS_EQUALS = "<=";

    /**
     * description: 检查是否符合条件 <br/>
     * author: gs   <br/>
     * date: 2021/1/7 <br/>
     * params: [form, flowNode, flowNodeService] <br/>
     * return: boolean
     */
    public static boolean check(Form form, FlowNode flowNode, IFlowNodeService flowNodeService, IFormService formService){
        //拿到目标表
        String target = form.getTarget();
        //拿到目标表主键
        String targetId = formService.renderPrimaryKeyColumn(target);
        //拿到目标表主键值
        String id = form.getId();
        //拿到需要查的字段
        String field = flowNode.getField();
        //拿到符号
        String sign = flowNode.getSign();
        //拿到节点中的值
        Double count = flowNode.getCount();
        //判断符号、count和field是否为空
        if(ObjectUtils.isEmpty(field) || ObjectUtils.isEmpty(sign) || count == null || count ==0){
            return false;
        }
        //组织到map中
        Map<String,String> params = new HashMap<>();
        params.put("target", target);
        params.put("targetId", targetId);
        params.put("id", id);
        params.put("field", field);
        //查询出数据
        String result = flowNodeService.getTargetValue(params);
        //转换
        double value = Double.valueOf(result);
        //判断符号
        if(GREATE_EQUALS.equals(sign)){
            return value >= count;
        }else{
            return value <= count;
        }
    }
}
